package com.hades.xq.miaoshao.service.impl;

import com.hades.xq.miaoshao.dao.UserDOMapper;
import com.hades.xq.miaoshao.dao.UserPasswordDOMapper;
import com.hades.xq.miaoshao.dataobject.UserDO;
import com.hades.xq.miaoshao.dataobject.UserPasswordDO;
import com.hades.xq.miaoshao.error.BusinessException;
import com.hades.xq.miaoshao.error.EmBusinessError;
import com.hades.xq.miaoshao.service.UserService;
import com.hades.xq.miaoshao.service.model.UserModel;
import com.hades.xq.miaoshao.validator.ValidatorImpl;
import com.hades.xq.miaoshao.validator.ValidatorResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDOMapper userDOMapper;

    @Autowired
    private UserPasswordDOMapper userPasswordDOMapper;

    @Autowired
    private ValidatorImpl validator;

    @Override
    public UserModel validateLogin(String telphpne, String encrptPassword) throws BusinessException {
        UserDO userDO = userDOMapper.selectByTelphone(telphpne);
        if (userDO == null) {
            throw new BusinessException(EmBusinessError.USER_LOGIN_FIAL);
        }
        log.info("登录校验的手机号码和密码：" + telphpne + " , " + encrptPassword);
        UserPasswordDO userPasswordDO = userPasswordDOMapper.selectByUserID(userDO.getId());
        log.info("数据库内用户密码：" + userPasswordDO.getEncriptPassowrd());
        UserModel userModel = convertFromDataObject(userDO, userPasswordDO);
        if (!StringUtils.equals(encrptPassword, userModel.getEncriptPassowrd())) {
            throw new BusinessException(EmBusinessError.USER_LOGIN_FIAL);
        }
        return userModel;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void reginster(UserModel userModel) throws BusinessException {
        if (userModel == null) {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR);
        }
//        if (StringUtils.isEmpty(userModel.getName()) ||
//                userModel.getGender() == null ||
//                userModel.getAge() == null ||
//                StringUtils.isEmpty(userModel.getTelphone())) {
//            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR);
//        }
        //采用ValidatorImpl解决入参校验
        ValidatorResult validatorResult = validator.validator(userModel);
        if (validatorResult.isHasErrors()) {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, validatorResult.getErrMsg());
        }


        UserDO userDO = convertFromModel(userModel);

        try {
            userDOMapper.insertSelective(userDO);
        } catch (Exception ex) {
            log.error("UserServiceImpl.reginster()->" + ex.getMessage());
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, "手机号码已注册");
        }

        userModel.setId(userDO.getId());

        UserPasswordDO userPasswordDO = convertPasswordFromModel(userModel);
        userPasswordDOMapper.insertSelective(userPasswordDO);

        return;
    }

    private UserPasswordDO convertPasswordFromModel(UserModel userModel) {
        if (userModel == null) {
            return null;
        }
        UserPasswordDO userPasswordDO = new UserPasswordDO();
        userPasswordDO.setEncriptPassowrd(userModel.getEncriptPassowrd());
        userPasswordDO.setUserId(userModel.getId());
        return userPasswordDO;
    }

    private UserDO convertFromModel(UserModel userModel) {
        if (userModel == null) {
            return null;
        }
        UserDO userDO = new UserDO();
        BeanUtils.copyProperties(userModel, userDO);
        return userDO;
    }

    @Override
    public UserModel getUserById(Integer userId) {

        UserDO userDO = userDOMapper.selectByPrimaryKey(userId);

        if (userDO == null) {
            return null;
        }

        UserPasswordDO userPasswordDO = userPasswordDOMapper.selectByUserID(userDO.getId());

        return convertFromDataObject(userDO, userPasswordDO);
    }

    private UserModel convertFromDataObject(UserDO userDO, UserPasswordDO userPasswordDO) {
        if (userDO == null) {
            return null;
        }
        UserModel userModel = new UserModel();
        BeanUtils.copyProperties(userDO, userModel);
        if (userPasswordDO != null) {
            userModel.setEncriptPassowrd(userPasswordDO.getEncriptPassowrd());
        }
        return userModel;
    }

}